package com.dap.dubbo.rpc.filter;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;
import com.dap.dubbo.rpc.config.RedisConfig;
import redis.clients.jedis.Jedis;

/**
 * 将服务信息存入Redis
 */

@Activate(group = {Constants.CONSUMER},order = -10)
public class CommonDataFilter  implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        Jedis jedis = RedisConfig.getJedis();

        String resourceName = getResourceName(invoker, invocation);

        jedis.incr(resourceName);

        jedis.close();

        Result invoke = invoker.invoke(invocation);
        return invoke;
    }

    /**
     * 获取当前服务方法信息
     *
     * @param invoker
     * @param invocation
     * @return
     */
    private static String getResourceName(Invoker<?> invoker, Invocation invocation) {
        //服务名
        String interfaceName = invoker.getInterface().getName();
        //组名
        String application_group = invoker.getUrl().getParameter("group");
        String application = invoker.getUrl().getParameter("application");

        //拼接
        String resourceName =  "FOP:Application:"+application+"/Interface:"+interfaceName+"/Group:"+application_group;

        return resourceName;

    }
}
